<?php
class ComAction extends Action{
  public function class_manage(){
	  $this->display();
  }
  //获取父类
  public function get_parid(){
    $item_arr=create_cascade_item("syscolumn",0,"col_name");
    $option_str=show_cascade_item($item_arr);
	//count($option_str,COUNT_RECURSIVE) 计算多维数组元素个数
	//$this->assign("option_num",count($item_arr, COUNT_RECURSIVE));  
    echo $option_str;
  }
  //获取子类
  public function get_child(){
      $parid=intval($_GET["parid"]);
	  $syscolumn=M("syscolumn");
	  $child_option_arr=$syscolumn->where("parid=$parid")->order("sequence desc")->findAll();
	  echo json_encode($child_option_arr);
  }
  //保存子类
  public function save_child(){
	  $parid=$_GET["parid"];
	  $typename=urldecode($_GET["typename"]);
	  $syscolumn=D("syscolumn");
	  $maxsequence=$syscolumn->where("parid=$parid")->max("sequence");
	  $sequence=$maxsequence+1;
	  $data["col_name"]=$typename;
	  $data["sequence"]=$sequence;
	  $data["parid"]=$parid;
	  $syscolumn->add($data);
	  echo 1;
  }
  //获取typename 
  public function gettypename2typeid(){
	  checkget();
	  $typeid=$_GET["typeid"];
	  $syscolumn=M("syscolumn");
	  echo $syscolumn->where("id=$typeid")->getField("col_name");
  }
  //保存修改后的类别名称
  public function updatetypename(){
	  checkpost();
	  $typeid=$_POST["typeid"];
	  $new_type_name=$_POST["new_type_name"];
	  $syscolumn=M("syscolumn");
	  echo  $syscolumn->where("id=$typeid")->setField("col_name",$new_type_name)?1:0; 
  }
  //向上
  public function up(){
	 checkget();
	 $typeid=$_GET["typeid"];
	 $syscolumn=M("syscolumn");
	 $parid=$syscolumn->where("id=$typeid")->getField("parid");
	 $cur_seq=$syscolumn->where("id=$typeid")->getField("sequence");
	 $map["parid"]=$parid;
	 $map["sequence"]=array("gt",$cur_seq);
	 
	 $up_info=$syscolumn->where($map)->order("sequence asc")->field("id,sequence")->find();

	 $up_id=$up_info["id"];
	 $up_seq=$up_info["sequence"];
	 

	 $syscolumn->where("id=$typeid")->setField("sequence",$up_seq);
	 $syscolumn->where("id=$up_id")->setField("sequence",$cur_seq);
  }
  public function down(){
	 checkget();
	 $typeid=$_GET["typeid"];
	 $syscolumn=M("syscolumn");
	 $parid=$syscolumn->where("id=$typeid")->getField("parid");
	 $cur_seq=$syscolumn->where("id=$typeid")->getField("sequence");
	 $map["parid"]=$parid;
	 $map["sequence"]=array("lt",$cur_seq);
	 
	 $down_info=$syscolumn->where($map)->order("sequence desc")->field("id,sequence")->find();

	 $down_id=$down_info["id"];
	 $down_seq=$down_info["sequence"];
	 

	 $syscolumn->where("id=$typeid")->setField("sequence",$down_seq);
	 $syscolumn->where("id=$down_id")->setField("sequence",$cur_seq);
  }

}
?>